<template>
  <div class="home-container">
    <div class="home-header">
      <h1>欢迎，{{ userInfo?.nickname || userInfo?.username }}！</h1>
      <div class="header-buttons">
        <button @click="showCartModal" class="cart-btn">
          🛒 购物车
          <span v-if="cartItemCount > 0" class="cart-badge">{{ cartItemCount }}</span>
        </button>
        <button @click="goToOrders" class="orders-btn">我的订单</button>
        <button @click="goToUsers" class="users-btn">用户列表</button>
        <button @click="handleLogout" class="logout-btn">退出登录</button>
      </div>
    </div>

    <div class="book-list-container">
      <div class="book-list-header">
        <h2>图书列表</h2>
        <div class="header-actions">
          <div class="search-box">
            <input
              v-model="searchName"
              type="text"
              placeholder="搜索图书名称..."
              class="search-input"
              @keyup.enter="loadBooks"
            />
            <button @click="loadBooks" class="search-btn">搜索</button>
            <button @click="resetSearch" class="reset-btn">重置</button>
          </div>
          <button @click="showAddModal" class="add-btn">+ 添加图书</button>
        </div>
      </div>

      <div v-if="loading" class="loading">加载中...</div>
      <div v-else-if="errorMessage" class="error-message">{{ errorMessage }}</div>
      <div v-else class="book-table-container">
        <table class="book-table">
          <thead>
            <tr>
              <th>ID</th>
              <th>图书名称</th>
              <th>作者</th>
              <th>分类</th>
              <th>价格</th>
              <th>库存</th>
              <th>出版社</th>
              <th>状态</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            <tr v-if="bookList.length === 0">
              <td colspan="9" class="empty-data">暂无数据</td>
            </tr>
            <tr v-for="book in bookList" :key="book.id">
              <td>{{ book.id }}</td>
              <td>{{ book.name }}</td>
              <td>{{ book.author || '-' }}</td>
              <td>{{ book.category || '-' }}</td>
              <td>¥{{ book.price ? book.price.toFixed(2) : '0.00' }}</td>
              <td>{{ book.stock || 0 }}</td>
              <td>{{ book.publisher || '-' }}</td>
              <td>
                <span :class="['status-badge', book.status === 1 ? 'status-on' : 'status-off']">
                  {{ book.status === 1 ? '在售' : '下架' }}
                </span>
              </td>
              <td>
                <div class="action-buttons">
                  <button
                    v-if="book.status === 1"
                    @click="handleAddToCart(book)"
                    class="add-cart-btn"
                    :disabled="addingToCartId === book.id || book.stock === 0"
                    :title="book.stock === 0 ? '库存不足' : '加入购物车'"
                  >
                    <span v-if="addingToCartId === book.id">添加中...</span>
                    <span v-else-if="book.stock === 0">缺货</span>
                    <span v-else>加入购物车</span>
                  </button>
                  <button
                    v-if="book.status === 1"
                    @click="handleOffline(book)"
                    class="offline-btn"
                    :disabled="offliningId === book.id"
                  >
                    <span v-if="offliningId === book.id">下架中...</span>
                    <span v-else>下架</span>
                  </button>
                  <span v-if="book.status === 0" class="disabled-text">已下架</span>
                </div>
              </td>
            </tr>
          </tbody>
        </table>

        <div class="pagination" v-if="pagination.total > 0">
          <button
            @click="changePage(pagination.current - 1)"
            :disabled="pagination.current <= 1"
            class="page-btn"
          >
            上一页
          </button>
          <span class="page-info">
            第 {{ pagination.current }} / {{ pagination.pages }} 页，共 {{ pagination.total }} 条
          </span>
          <button
            @click="changePage(pagination.current + 1)"
            :disabled="pagination.current >= pagination.pages"
            class="page-btn"
          >
            下一页
          </button>
        </div>
      </div>
    </div>

    <!-- 添加图书模态框 -->
    <div v-if="showModal" class="modal-overlay" @click="closeModal">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3>添加图书</h3>
          <button @click="closeModal" class="close-btn">×</button>
        </div>
        <form @submit.prevent="handleAddBook" class="modal-body">
          <div class="form-row">
            <div class="form-group">
              <label>图书名称 <span class="required">*</span></label>
              <input
                v-model="bookForm.name"
                type="text"
                placeholder="请输入图书名称"
                required
                :class="{ 'error': errors.name }"
              />
              <span v-if="errors.name" class="error-text">{{ errors.name }}</span>
            </div>
            <div class="form-group">
              <label>作者</label>
              <input
                v-model="bookForm.author"
                type="text"
                placeholder="请输入作者"
              />
            </div>
          </div>

          <div class="form-row">
            <div class="form-group">
              <label>价格 <span class="required">*</span></label>
              <input
                v-model.number="bookForm.price"
                type="number"
                step="0.01"
                min="0"
                placeholder="请输入价格"
                required
                :class="{ 'error': errors.price }"
              />
              <span v-if="errors.price" class="error-text">{{ errors.price }}</span>
            </div>
            <div class="form-group">
              <label>库存 <span class="required">*</span></label>
              <input
                v-model.number="bookForm.stock"
                type="number"
                min="0"
                placeholder="请输入库存"
                required
                :class="{ 'error': errors.stock }"
              />
              <span v-if="errors.stock" class="error-text">{{ errors.stock }}</span>
            </div>
          </div>

          <div class="form-row">
            <div class="form-group">
              <label>分类</label>
              <input
                v-model="bookForm.category"
                type="text"
                placeholder="请输入分类"
              />
            </div>
            <div class="form-group">
              <label>出版社</label>
              <input
                v-model="bookForm.publisher"
                type="text"
                placeholder="请输入出版社"
              />
            </div>
          </div>

          <div class="form-row">
            <div class="form-group">
              <label>ISBN</label>
              <input
                v-model="bookForm.isbn"
                type="text"
                placeholder="请输入ISBN号"
              />
            </div>
            <div class="form-group">
              <label>出版日期</label>
              <input
                v-model="bookForm.publishDate"
                type="date"
              />
            </div>
          </div>

          <div class="form-group full-width">
            <label>描述</label>
            <textarea
              v-model="bookForm.description"
              rows="3"
              placeholder="请输入图书描述"
            ></textarea>
          </div>

          <div v-if="submitError" class="error-alert">
            {{ submitError }}
          </div>

          <div class="modal-footer">
            <button type="button" @click="closeModal" class="cancel-btn">取消</button>
            <button type="submit" class="submit-btn" :disabled="submitting">
              <span v-if="submitting">提交中...</span>
              <span v-else>提交</span>
            </button>
          </div>
        </form>
      </div>
    </div>

    <!-- 购物车模态框 -->
    <div v-if="showCart" class="modal-overlay" @click="closeCartModal">
      <div class="modal-content cart-modal" @click.stop>
        <div class="modal-header">
          <h3>我的购物车</h3>
          <button @click="closeCartModal" class="close-btn">×</button>
        </div>
        <div class="cart-body">
          <div v-if="cartLoading" class="loading">加载中...</div>
          <div v-else-if="cartError" class="error-message">{{ cartError }}</div>
          <div v-else-if="cartList.length === 0" class="empty-cart">
            <p>购物车是空的</p>
            <button @click="closeCartModal" class="go-shopping-btn">去购物</button>
          </div>
          <div v-else class="cart-list">
            <table class="cart-table">
              <thead>
                <tr>
                  <th>图书名称</th>
                  <th>作者</th>
                  <th>单价</th>
                  <th>数量</th>
                  <th>小计</th>
                  <th>操作</th>
                </tr>
              </thead>
              <tbody>
                <tr v-for="item in cartList" :key="item.cartId">
                  <td>{{ item.bookName }}</td>
                  <td>{{ item.author || '-' }}</td>
                  <td>¥{{ item.price ? item.price.toFixed(2) : '0.00' }}</td>
                  <td>
                    <div class="quantity-control">
                      <button
                        @click="decreaseQuantity(item)"
                        :disabled="item.quantity <= 1 || updatingCartId === item.cartId"
                        class="quantity-btn"
                      >
                        -
                      </button>
                      <span class="quantity-value">{{ item.quantity }}</span>
                      <button
                        @click="increaseQuantity(item)"
                        :disabled="item.quantity >= item.stock || updatingCartId === item.cartId"
                        class="quantity-btn"
                        :title="item.quantity >= item.stock ? '库存不足' : ''"
                      >
                        +
                      </button>
                    </div>
                  </td>
                  <td class="subtotal">¥{{ item.subtotal ? item.subtotal.toFixed(2) : '0.00' }}</td>
                  <td>
                    <button
                      @click="handleRemoveFromCart(item)"
                      :disabled="removingCartId === item.cartId"
                      class="remove-btn"
                    >
                      <span v-if="removingCartId === item.cartId">删除中...</span>
                      <span v-else>删除</span>
                    </button>
                  </td>
                </tr>
              </tbody>
              <tfoot>
                <tr>
                  <td colspan="4" class="total-label">总计：</td>
                  <td class="total-amount">¥{{ totalAmount.toFixed(2) }}</td>
                  <td></td>
                </tr>
              </tfoot>
            </table>
            <div class="cart-footer">
              <button @click="handleClearCart" class="clear-cart-btn" :disabled="clearingCart || creatingOrder">
                <span v-if="clearingCart">清空中...</span>
                <span v-else>清空购物车</span>
              </button>
              <button @click="handleCreateOrder" class="create-order-btn" :disabled="creatingOrder || cartList.length === 0">
                <span v-if="creatingOrder">生成订单中...</span>
                <span v-else>生成订单 (¥{{ totalAmount.toFixed(2) }})</span>
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive, computed } from 'vue'
import { useRouter } from 'vue-router'
import { getUserInfo, removeToken } from '../utils/auth'
import { getBookList, createBook, offlineBook } from '../api/book'
import { addToCart, getCartList, updateCartQuantity, removeFromCart, clearCart } from '../api/cart'
import { createOrder } from '../api/order'

const router = useRouter()
const userInfo = ref(null)
const bookList = ref([])
const loading = ref(false)
const errorMessage = ref('')
const searchName = ref('')

const pagination = ref({
  current: 1,
  size: 10,
  total: 0,
  pages: 0
})

// 添加图书相关
const showModal = ref(false)
const submitting = ref(false)
const submitError = ref('')
const bookForm = reactive({
  name: '',
  author: '',
  isbn: '',
  price: null,
  stock: null,
  description: '',
  category: '',
  publisher: '',
  publishDate: '',
  status: 1
})

const errors = reactive({
  name: '',
  price: '',
  stock: ''
})

// 下架图书相关
const offliningId = ref(null)

// 加入购物车相关
const addingToCartId = ref(null)

// 购物车相关
const showCart = ref(false)
const cartList = ref([])
const cartLoading = ref(false)
const cartError = ref('')
const updatingCartId = ref(null)
const removingCartId = ref(null)
const clearingCart = ref(false)
const creatingOrder = ref(false)

// 计算购物车商品数量
const cartItemCount = computed(() => {
  return cartList.value.reduce((sum, item) => sum + item.quantity, 0)
})

// 计算总金额
const totalAmount = computed(() => {
  return cartList.value.reduce((sum, item) => sum + (item.subtotal || 0), 0)
})

onMounted(() => {
  userInfo.value = getUserInfo()
  if (!userInfo.value) {
    router.push('/login')
  } else {
    loadBooks()
  }
})

const loadBooks = async () => {
  loading.value = true
  errorMessage.value = ''
  
  try {
    const res = await getBookList({
      current: pagination.value.current,
      size: pagination.value.size,
      name: searchName.value || undefined,
      status: 1 // 只查询在售的图书
    })
    
    if (res.code === 200 && res.data) {
      bookList.value = res.data.records || []
      pagination.value = {
        current: res.data.current || 1,
        size: res.data.size || 10,
        total: res.data.total || 0,
        pages: res.data.pages || 0
      }
    }
  } catch (error) {
    errorMessage.value = error.message || '加载图书列表失败'
    console.error('加载图书列表失败:', error)
  } finally {
    loading.value = false
  }
}

const changePage = (page) => {
  if (page < 1 || page > pagination.value.pages) {
    return
  }
  pagination.value.current = page
  loadBooks()
}

const resetSearch = () => {
  searchName.value = ''
  pagination.value.current = 1
  loadBooks()
}

const handleLogout = () => {
  removeToken()
  router.push('/login')
}

// 跳转到订单页面
const goToOrders = () => {
  router.push('/orders')
}

// 跳转到用户列表页面
const goToUsers = () => {
  router.push('/users')
}

// 添加图书相关方法
const showAddModal = () => {
  showModal.value = true
  resetForm()
}

const closeModal = () => {
  showModal.value = false
  resetForm()
}

const resetForm = () => {
  bookForm.name = ''
  bookForm.author = ''
  bookForm.isbn = ''
  bookForm.price = null
  bookForm.stock = null
  bookForm.description = ''
  bookForm.category = ''
  bookForm.publisher = ''
  bookForm.publishDate = ''
  bookForm.status = 1
  errors.name = ''
  errors.price = ''
  errors.stock = ''
  submitError.value = ''
}

const validateForm = () => {
  let valid = true
  errors.name = ''
  errors.price = ''
  errors.stock = ''

  if (!bookForm.name || !bookForm.name.trim()) {
    errors.name = '图书名称不能为空'
    valid = false
  }

  if (bookForm.price === null || bookForm.price === undefined || bookForm.price < 0) {
    errors.price = '价格必须大于等于0'
    valid = false
  }

  if (bookForm.stock === null || bookForm.stock === undefined || bookForm.stock < 0) {
    errors.stock = '库存必须大于等于0'
    valid = false
  }

  return valid
}

const handleAddBook = async () => {
  submitError.value = ''
  
  if (!validateForm()) {
    return
  }

  submitting.value = true

  try {
    const data = {
      name: bookForm.name.trim(),
      author: bookForm.author || undefined,
      isbn: bookForm.isbn || undefined,
      price: bookForm.price,
      stock: bookForm.stock,
      description: bookForm.description || undefined,
      category: bookForm.category || undefined,
      publisher: bookForm.publisher || undefined,
      publishDate: bookForm.publishDate || undefined,
      status: bookForm.status
    }

    const res = await createBook(data)
    
    if (res.code === 200) {
      // 添加成功，关闭模态框并刷新列表
      closeModal()
      loadBooks()
      alert('图书添加成功！')
    }
  } catch (error) {
    submitError.value = error.message || '添加图书失败，请稍后重试'
    console.error('添加图书失败:', error)
  } finally {
    submitting.value = false
  }
}

// 加入购物车
const handleAddToCart = async (book) => {
  // 检查库存
  if (book.stock === 0) {
    alert('库存不足，无法加入购物车')
    return
  }

  addingToCartId.value = book.id

  try {
    const res = await addToCart({
      bookId: book.id,
      quantity: 1
    })
    
    if (res.code === 200) {
      alert(`《${book.name}》已加入购物车！`)
      // 如果购物车已打开，刷新购物车列表
      if (showCart.value) {
        loadCartList()
      }
    }
  } catch (error) {
    alert(error.message || '加入购物车失败，请稍后重试')
    console.error('加入购物车失败:', error)
  } finally {
    addingToCartId.value = null
  }
}

// 显示购物车
const showCartModal = async () => {
  showCart.value = true
  await loadCartList()
}

// 关闭购物车
const closeCartModal = () => {
  showCart.value = false
}

// 加载购物车列表
const loadCartList = async () => {
  cartLoading.value = true
  cartError.value = ''
  
  try {
    const res = await getCartList()
    if (res.code === 200) {
      cartList.value = res.data || []
    }
  } catch (error) {
    cartError.value = error.message || '加载购物车失败'
    console.error('加载购物车失败:', error)
  } finally {
    cartLoading.value = false
  }
}

// 增加数量
const increaseQuantity = async (item) => {
  if (item.quantity >= item.stock) {
    alert('库存不足')
    return
  }

  updatingCartId.value = item.cartId
  const newQuantity = item.quantity + 1

  try {
    const res = await updateCartQuantity(item.cartId, newQuantity)
    if (res.code === 200) {
      await loadCartList()
    }
  } catch (error) {
    alert(error.message || '更新数量失败')
    console.error('更新数量失败:', error)
  } finally {
    updatingCartId.value = null
  }
}

// 减少数量
const decreaseQuantity = async (item) => {
  if (item.quantity <= 1) {
    return
  }

  updatingCartId.value = item.cartId
  const newQuantity = item.quantity - 1

  try {
    const res = await updateCartQuantity(item.cartId, newQuantity)
    if (res.code === 200) {
      await loadCartList()
    }
  } catch (error) {
    alert(error.message || '更新数量失败')
    console.error('更新数量失败:', error)
  } finally {
    updatingCartId.value = null
  }
}

// 删除商品
const handleRemoveFromCart = async (item) => {
  if (!confirm(`确定要从购物车删除《${item.bookName}》吗？`)) {
    return
  }

  removingCartId.value = item.cartId

  try {
    const res = await removeFromCart(item.cartId)
    if (res.code === 200) {
      await loadCartList()
      alert('删除成功')
    }
  } catch (error) {
    alert(error.message || '删除失败')
    console.error('删除失败:', error)
  } finally {
    removingCartId.value = null
  }
}

// 清空购物车
const handleClearCart = async () => {
  if (!confirm('确定要清空购物车吗？')) {
    return
  }

  clearingCart.value = true

  try {
    const res = await clearCart()
    if (res.code === 200) {
      await loadCartList()
      alert('购物车已清空')
    }
  } catch (error) {
    alert(error.message || '清空失败')
    console.error('清空失败:', error)
  } finally {
    clearingCart.value = false
  }
}

// 生成订单
const handleCreateOrder = async () => {
  if (cartList.value.length === 0) {
    alert('购物车为空，无法生成订单')
    return
  }

  if (!confirm(`确定要生成订单吗？\n订单总金额：¥${totalAmount.value.toFixed(2)}`)) {
    return
  }

  creatingOrder.value = true

  try {
    const res = await createOrder()
    
    if (res.code === 200 && res.data) {
      const order = res.data
      alert(`订单创建成功！\n订单号：${order.orderNo}\n订单金额：¥${order.totalAmount.toFixed(2)}`)
      
      // 刷新购物车列表（订单创建后购物车会被清空）
      await loadCartList()
      
      // 可以选择关闭购物车模态框
      // closeCartModal()
    }
  } catch (error) {
    alert(error.message || '生成订单失败，请稍后重试')
    console.error('生成订单失败:', error)
  } finally {
    creatingOrder.value = false
  }
}

// 下架图书
const handleOffline = async (book) => {
  // 确认对话框
  if (!confirm(`确定要下架《${book.name}》吗？`)) {
    return
  }

  offliningId.value = book.id

  try {
    const res = await offlineBook(book.id)
    
    if (res.code === 200) {
      // 下架成功，刷新列表
      alert('图书下架成功！')
      loadBooks()
    }
  } catch (error) {
    alert(error.message || '下架图书失败，请稍后重试')
    console.error('下架图书失败:', error)
  } finally {
    offliningId.value = null
  }
}
</script>

<style scoped>
.home-container {
  min-height: 100vh;
  padding: 40px;
}

.home-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: white;
  padding: 24px 32px;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  margin-bottom: 24px;
}

.home-header h1 {
  color: #333;
  font-size: 28px;
}

.header-buttons {
  display: flex;
  gap: 12px;
  align-items: center;
}

.cart-btn {
  padding: 10px 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s;
  position: relative;
}

.cart-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.cart-badge {
  position: absolute;
  top: -8px;
  right: -8px;
  background: #f56565;
  color: white;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: bold;
}

.orders-btn,
.users-btn {
  padding: 10px 20px;
  background: white;
  color: #667eea;
  border: 2px solid #667eea;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s;
}

.orders-btn:hover,
.users-btn:hover {
  background: #667eea;
  color: white;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.logout-btn {
  padding: 10px 20px;
  background: #f56565;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
}

.logout-btn:hover {
  background: #e53e3e;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(245, 101, 101, 0.3);
}

.book-list-container {
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  padding: 24px;
}

.book-list-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 2px solid #f0f0f0;
}

.book-list-header h2 {
  color: #333;
  font-size: 24px;
  margin: 0;
}

.header-actions {
  display: flex;
  gap: 16px;
  align-items: center;
}

.search-box {
  display: flex;
  gap: 12px;
  align-items: center;
}

.add-btn {
  padding: 10px 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s;
  white-space: nowrap;
}

.add-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.search-input {
  padding: 8px 16px;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  font-size: 14px;
  outline: none;
  transition: all 0.3s;
  width: 200px;
}

.search-input:focus {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.search-btn,
.reset-btn {
  padding: 8px 16px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
}

.search-btn {
  background: #667eea;
  color: white;
}

.search-btn:hover {
  background: #5568d3;
  transform: translateY(-1px);
}

.reset-btn {
  background: #e0e0e0;
  color: #333;
}

.reset-btn:hover {
  background: #d0d0d0;
}

.loading,
.error-message {
  text-align: center;
  padding: 40px;
  color: #666;
  font-size: 16px;
}

.error-message {
  color: #f56565;
}

.book-table-container {
  overflow-x: auto;
}

.book-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.book-table thead {
  background: #f8f9fa;
}

.book-table th {
  padding: 12px;
  text-align: left;
  font-weight: 600;
  color: #333;
  border-bottom: 2px solid #e0e0e0;
}

.book-table td {
  padding: 12px;
  border-bottom: 1px solid #f0f0f0;
  color: #666;
}

.book-table tbody tr:hover {
  background: #f8f9fa;
}

.empty-data {
  text-align: center;
  color: #999;
  padding: 40px !important;
}

.status-badge {
  display: inline-block;
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

.status-on {
  background: #c6f6d5;
  color: #22543d;
}

.status-off {
  background: #fed7d7;
  color: #742a2a;
}

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 16px;
  margin-top: 24px;
  padding-top: 24px;
  border-top: 1px solid #f0f0f0;
}

.page-btn {
  padding: 8px 16px;
  border: 1px solid #e0e0e0;
  background: white;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
}

.page-btn:hover:not(:disabled) {
  background: #667eea;
  color: white;
  border-color: #667eea;
}

.page-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.page-info {
  color: #666;
  font-size: 14px;
}

/* 操作列样式 */
.action-buttons {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.add-cart-btn {
  padding: 6px 16px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  transition: all 0.3s;
  white-space: nowrap;
}

.add-cart-btn:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
}

.add-cart-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  background: #ccc;
}

.offline-btn {
  padding: 6px 16px;
  background: #f56565;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  transition: all 0.3s;
  white-space: nowrap;
}

.offline-btn:hover:not(:disabled) {
  background: #e53e3e;
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(245, 101, 101, 0.3);
}

.offline-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.disabled-text {
  color: #999;
  font-size: 13px;
}

/* 模态框样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  padding: 20px;
}

.modal-content {
  background: white;
  border-radius: 12px;
  width: 100%;
  max-width: 700px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  border-bottom: 1px solid #f0f0f0;
}

.modal-header h3 {
  margin: 0;
  color: #333;
  font-size: 20px;
}

.close-btn {
  background: none;
  border: none;
  font-size: 28px;
  color: #999;
  cursor: pointer;
  line-height: 1;
  padding: 0;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  transition: all 0.3s;
}

.close-btn:hover {
  background: #f0f0f0;
  color: #333;
}

.modal-body {
  padding: 24px;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  margin-bottom: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
}

.form-group.full-width {
  grid-column: 1 / -1;
}

.form-group label {
  margin-bottom: 8px;
  color: #333;
  font-size: 14px;
  font-weight: 500;
}

.required {
  color: #f56565;
}

.form-group input,
.form-group textarea {
  padding: 10px 12px;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  font-size: 14px;
  outline: none;
  transition: all 0.3s;
  font-family: inherit;
}

.form-group input:focus,
.form-group textarea:focus {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.form-group input.error,
.form-group textarea.error {
  border-color: #f56565;
}

.error-text {
  color: #f56565;
  font-size: 12px;
  margin-top: 4px;
}

.error-alert {
  background: #fee;
  border: 1px solid #fcc;
  color: #c33;
  padding: 12px;
  border-radius: 8px;
  margin-bottom: 20px;
  font-size: 14px;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 24px;
  padding-top: 20px;
  border-top: 1px solid #f0f0f0;
}

.modal-footer .cancel-btn,
.modal-footer .submit-btn {
  padding: 10px 24px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s;
}

.modal-footer .cancel-btn {
  background: #e0e0e0;
  color: #333;
}

.modal-footer .cancel-btn:hover {
  background: #d0d0d0;
}

.modal-footer .submit-btn {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.modal-footer .submit-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.modal-footer .submit-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* 购物车模态框样式 */
.cart-modal {
  max-width: 900px;
  max-height: 90vh;
}

.cart-body {
  padding: 24px;
  max-height: calc(90vh - 120px);
  overflow-y: auto;
}

.empty-cart {
  text-align: center;
  padding: 60px 20px;
  color: #999;
}

.empty-cart p {
  font-size: 16px;
  margin-bottom: 20px;
}

.go-shopping-btn {
  padding: 10px 24px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
}

.go-shopping-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.cart-list {
  margin-top: 20px;
}

.cart-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.cart-table thead {
  background: #f8f9fa;
}

.cart-table th {
  padding: 12px;
  text-align: left;
  font-weight: 600;
  color: #333;
  border-bottom: 2px solid #e0e0e0;
}

.cart-table td {
  padding: 12px;
  border-bottom: 1px solid #f0f0f0;
  color: #666;
}

.cart-table tbody tr:hover {
  background: #f8f9fa;
}

.quantity-control {
  display: flex;
  align-items: center;
  gap: 8px;
}

.quantity-btn {
  width: 28px;
  height: 28px;
  border: 1px solid #e0e0e0;
  background: white;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s;
}

.quantity-btn:hover:not(:disabled) {
  background: #667eea;
  color: white;
  border-color: #667eea;
}

.quantity-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.quantity-value {
  min-width: 30px;
  text-align: center;
  font-weight: 500;
}

.subtotal {
  color: #667eea;
  font-weight: 600;
}

.remove-btn {
  padding: 6px 12px;
  background: #f56565;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
  transition: all 0.3s;
}

.remove-btn:hover:not(:disabled) {
  background: #e53e3e;
  transform: translateY(-1px);
}

.remove-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.cart-table tfoot {
  background: #f8f9fa;
  font-weight: 600;
}

.total-label {
  text-align: right;
  font-size: 16px;
  color: #333;
}

.total-amount {
  color: #667eea;
  font-size: 18px;
}

.cart-footer {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #f0f0f0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.clear-cart-btn {
  padding: 10px 20px;
  background: #e0e0e0;
  color: #333;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
}

.clear-cart-btn:hover:not(:disabled) {
  background: #d0d0d0;
}

.clear-cart-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.create-order-btn {
  padding: 12px 32px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 16px;
  font-weight: 600;
  transition: all 0.3s;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.create-order-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(102, 126, 234, 0.4);
}

.create-order-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}
</style>

